<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">

		<el-container v-loading="loading">

			<el-main style="padding:0 20px 20px 20px">

				<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">

					<el-form-item label="文案" prop="moment_content">
						<el-input type="textarea" rows="5" v-model="form.moment_content" placeholder="请输入活动名称"
							clearable>
						</el-input>
					</el-form-item>

					<el-form-item label="图片" prop="moment_media_list">
						<sc-oss-upload-multiple limit="8" v-model="form.moment_media_list"></sc-oss-upload-multiple>
					</el-form-item>

				</el-form>

			</el-main>
		</el-container>

		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show' && !loading" type="primary" :loading="isSaveing" @click="submit()">保
				存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				defaultTime: [
					new Date(2000, 1, 1, 0, 0, 0),
					new Date(2000, 2, 1, 23, 59, 59),
				],
				loading: true,
				mode: "add",
				titleMap: {
					add: '新增发布',
					edit: '编辑发布',
					show: '查看发布'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id: "",
					moment_media_list: [],
					moment_content: '',
				},
				//验证规则
				rules: {
					not_before: [{
						required: true,
						message: '选择活动开始日期'
					}],
					name: [{
						required: true,
						message: '输入活动名称'
					}]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					label: "name",
					emitPath: false,
					checkStrictly: true
				}
			}
		},
		mounted() {
			this.getGroup()
		},
		methods: {
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				this.loading = false;
				return this
			},
			//加载树数据
			async getGroup() {


			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res;
						if (this.mode == 'add') {
							res = await this.$API.system.moment.create.post(this.form);
						} else {
							res = await this.$API.system.moment.update.post(this.form);
						}
						this.isSaveing = false;
						if (res.code == 0) {
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {
								type: 'error'
							})
						}
					}
				})
			},
			//表单注入数据
			setData(data) {
				this.loading = true
				const params = {
					id: data.id
				}
				setTimeout(async () => {
					var res = await this.$API.system.moment.info.get(params)
					this.form = Object.assign(this.form, res.data)
					this.loading = false
				}, 400)
			}
		}
	}
</script>

<style>
</style>
